// pages/city/city.js
var city = require('../../lib/city.js');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    city: '深圳市',
    cityData: {},
    hotCityData: [],
    _py: ["hot", "A", "B", "C", "D", "E", "F", "G", "H", "J", "K", "L", "M", "N", "P", "Q", "R", "S", "T", "W", "X", "Y", "Z"],
    pyHidden: true,
    showPy: '★'
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      cityData: city.all,
      hotCityData: city.hot
    });
  },

  //打开城市列表
  openCityList: function (e) {
    var cityListShow = !e.currentTarget.dataset.citylistshow;

    this.setData({
      cityListShow: cityListShow,
      listShow: !cityListShow
    });
  },

  /* 城市选择 BEGIN */
  //选择城市
  selectCity: function (e) {
    var dataset = e.currentTarget.dataset;

    wx.setStorageSync("userCityKey", dataset)
    wx.navigateBack()

    // this.setData({
    //   citySelected: dataset.fullname,
    //   cityListShow: false,
    //   listShow: true,
    //   location: {
    //     latitude: dataset.lat,
    //     longitude: dataset.lng
    //   }
    // });
  },
  touchstart: function (e) {
    this.setData({
      index: e.currentTarget.dataset.index,
      Mstart: e.changedTouches[0].pageX
    });
  },
  touchmove: function (e) {
    var history = this.data.historyList;
    var move = this.data.Mstart - e.changedTouches[0].pageX;
    history[this.data.index].x = move > 0 ? -move : 0;
    this.setData({
      historyList: history
    });
  },
  touchend: function (e) {
    var history = this.data.historyList;
    var move = this.data.Mstart - e.changedTouches[0].pageX;
    history[this.data.index].x = move > 100 ? -180 : 0;
    this.setData({
      historyList: history
    });
  },
  //获取文字信息
  getPy: function (e) {
    var showPy = e.target.id;
    if (showPy != undefined) {
      this.setData({
        pyHidden: false,
        showPy: showPy,
      })
    }
  },

  setPy: function (e) {
    this.setData({
      pyHidden: true,
      scrollTopId: this.data.showPy
    })
  },

  //滑动选择城市
  tMove: function (e) {
    var y = e.touches[0].clientY,
      offsettop = e.currentTarget.offsetTop;

    //判断选择区域,只有在选择区才会生效
    if (y > offsettop) {
      var num = parseInt((y - offsettop) / 12);
      var showPy = this.data._py[num];
      if (showPy != undefined) {
        this.setData({
          showPy: showPy
        })
      }
    };
  },

  //触发全部开始选择
  tStart: function () {
    this.setData({
      hidden: false
    })
  },

  //触发结束选择
  tEnd: function () {
    this.setData({
      hidden: true,
      scrollTopId: this.data.showPy
    })
  },
  /* 城市选择 END */


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  }
})